package com.web.server.controller;

import cn.com.sgcc.dlt698.Frame698;
import com.aayushatharva.brotli4j.Brotli4jLoader;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.web.SpringUtils;
import com.web.server.controller.echart.EChartGaps;
import com.web.server.db.DBUtil;
import com.web.server.util.Util;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.concurrent.Executors.newThreadPerTaskExecutor;

@RestController
@RequestMapping(value = "api/v1/data")
@Slf4j
public class SGWService {
    private final DBUtil dbUtil = new DBUtil();

    private final ObjectMapper mapper = new ObjectMapper();

    private MqttClient client;
    @Value("${mqtt.host}")
    public  String     host;
    @Value("${mqtt.username}")
    public  String     username;
    @Value("${mqtt.password}")
    public  String     password;
    @Value("${mqtt.clientId}")
    public  String     clientId;
    @Value("${mqtt.timeout}")
    public  int        timeOut;
    @Value("${mqtt.keepalive}")
    public  int        keepAlive;

    @Value("${sgw.topics}")
    private String sgwTopics;

    @Value("${sgw.topos.query.cron}")
    private String toposQueryCron;

    private final ExecutorService executor = new ThreadPoolExecutor(8, 16, 30, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10000));

    private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private final Pattern pattern = Pattern.compile(".*,(.+)$");

    static final String BODY_IN  = "<< {} {}";
    static final String BODY_OUT = ">> {} [{}]{}";

    private static final Map<String, String> OINames = new HashMap<>();

    static {
        OINames.put("0010", "正向有功电能");
        OINames.put("0020", "反向有功电能");
        OINames.put("0030", "组合无功1电能");
        OINames.put("0040", "组合无功2电能");
        OINames.put("0050", "第一象限无功总电能");
        OINames.put("0060", "第二象限无功总电能");
        OINames.put("0070", "第三象限无功总电能");
        OINames.put("0080", "第四象限无功总电能");

        OINames.put("1010", "正向有功最大需量及发生时间");
        OINames.put("1020", "反向有功最大需量及发生时间");

        OINames.put("2000", "电压");
        OINames.put("2001", "电流");
        OINames.put("2004", "有功功率");
        OINames.put("2005", "无功功率");
        OINames.put("2017", "有功需量");
        OINames.put("2018", "无功需量");
        OINames.put("200A", "功率因数");
    }

    public SGWService() {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    @PostConstruct
    public void init() {
        log.info("---- MQTT broker address: {}", host);
        log.info("---- Topos qurey cron: {}", toposQueryCron);

        Brotli4jLoader.ensureAvailability();

        String clientIdName = clientId.substring(0, 8);
        try {
            client = new MqttClient(host, clientIdName, new MemoryPersistence());

            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setUserName(username);
            connOpts.setPassword(password.toCharArray());
            connOpts.setConnectionTimeout(timeOut);
            connOpts.setKeepAliveInterval(keepAlive);
            connOpts.setAutomaticReconnect(true);

            client.setCallback(new MqttCallbackExtended() {

                @Override
                public void connectComplete(boolean reconnect, String serverURI) {
                    String[] topics = sgwTopics.split(";");

                    final int[] qos = new int[topics.length];

                    try {
                        client.subscribe(topics, qos);
                        log.info("MQTT connect completed, subscribe [\n    {}\n]", StringUtils.join(topics, "\n    "));
                    } catch (MqttException e) {
                        log.error("connectComplete failed", e);
                    }
                }

                @Override
                public void connectionLost(Throwable cause) {
                    log.error("---- connection lost. {}", cause.getMessage());
                }

                @Override
                public void messageArrived(String topic, MqttMessage message) {
                    messageArrived1(topic, message);
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {
                    //
                }
            });
            client.connect(connOpts);
        } catch (Exception e) {
            if (e instanceof MqttException)
                log.error("Failed connect MQTT broker. {}", e.getMessage());
            else
                log.error("Failed connect MQTT broker. ", e);

            try {
                client.disconnect();
            } catch (MqttException ignored) {
                // LUCENENET specific - removed unnecessary try/finally block
            }
            try {
                client.close();
            } catch (MqttException ignored) {
                // LUCENENET specific - removed unnecessary try/finally block
            }
            SpringUtils.exit();
        }
    }

    public void messageArrived1(String topic, MqttMessage message) {
        String[] topicList = topic.split("/");
        String   body      = new String(message.getPayload());

        if (body.contains("char data:") && body.endsWith("16")) {
            Matcher matcher = pattern.matcher(body);
            if (matcher.find()) {
                body = matcher.group(1);
            }
        }

        if (!body.startsWith("68") || !body.endsWith("16")) {
            return;
        }
        log.info("<-- /{} [{}] {}", body.length(), topic, body);

        String finalBody = body;
        executor.submit(() -> {
            try {
                dbUtil.dump(Frame698.parse(finalBody));
            } catch (Exception e) {
                log.error("process Frame698", e);
                return;
            }
        });
        //            String frame698Str;
        //
        //            frame698Str = "685C00C3051066350100006427D3850390601203000500202A0200006040020000604102000060420200000010020001015507050001147026531C07E904100837001C07E904100837031C07E90410083700010106005421560000532216";
        //            dbUtil.dump(Frame698.parse(frame698Str));
        //            frame698Str = "68390FC30510663501000064B05A850392601203000500202A02000060400200006041020000604202000150020200010030020101605507050001147026531C07E90410000F001C07E904100014021C07E90410000000010105000DF2495507050001147026531C07E90410001E001C07E904100023021C07E90410000F00010105000DF2505507050001147026531C07E90410002D001C07E904100032021C07E90410001E00010105000DF2545507050001147026531C07E904100100001C07E904100105021C07E90410002D00010105000DF25B5507050001147026531C07E90410010F001C07E904100114031C07E90410010000010105000DF2605507050001147026531C07E90410011E001C07E904100123031C07E90410010F00010105000DF2675507050001147026531C07E90410012D001C07E904100132031C07E90410011E00010105000DF26C5507050001147026531C07E904100200001C07E904100205021C07E90410012D00010105000DF2735507050001147026531C07E90410020F001C07E904100214021C07E90410020000010105000DF2785507050001147026531C07E90410021E001C07E904100223021C07E90410020F00010105000DF27E5507050001147026531C07E90410022D001C07E904100232021C07E90410021E00010105000DF2845507050001147026531C07E904100300001C07E904100305021C07E90410022D00010105000DF28A5507050001147026531C07E90410030F001C07E904100314021C07E90410030000010105000DF28F5507050001147026531C07E90410031E001C07E904100323021C07E90410030F00010105000DF2965507050001147026531C07E90410032D001C07E904100332021C07E90410031E00010105000DF29B5507050001147026531C07E904100400001C07E904100405021C07E90410032D00010105000DF2A15507050001147026531C07E90410040F001C07E904100414021C07E90410040000010105000DF2A85507050001147026531C07E90410041E001C07E904100423021C07E90410040F00010105000DF2AD5507050001147026531C07E90410042D001C07E904100432021C07E90410041E00010105000DF2B45507050001147026531C07E904100500001C07E904100505021C07E90410042D00010105000DF2B85507050001147026531C07E90410050F001C07E904100514031C07E90410050000010105000DF2BF5507050001147026531C07E90410051E001C07E904100523031C07E90410050F00010105000DF2C45507050001147026531C07E90410052D001C07E904100532031C07E90410051E00010105000DF2CA5507050001147026531C07E904100600001C07E904100605021C07E90410052D00010105000DF2D05507050001147026531C07E90410060F001C07E904100614021C07E90410060000010105000DF2D65507050001147026531C07E90410061E001C07E904100623021C07E90410060F00010105000DF2DD5507050001147026531C07E90410062D001C07E904100632021C07E90410061E00010105000DF2E35507050001147026531C07E904100700001C07E904100705021C07E90410062D00010105000DF2E85507050001147026531C07E90410070F001C07E904100714021C07E90410070000010105000DF2EB5507050001147026531C07E90410071E001C07E904100723021C07E90410070F00010105000DF2F25507050001147026531C07E90410072D001C07E904100732021C07E90410071E00010105000DF2F85507050001147026531C07E904100800001C07E904100805021C07E90410072D00010105000DF2FD5507050001147026531C07E90410080F001C07E904100814031C07E90410080000010105000DF3045507050001147026531C07E90410081E001C07E904100823021C07E90410080F00010105000DF30A5507050001147026531C07E90410082D001C07E904100832021C07E90410081E00010105000DF3135507050001147026531C07E904100900001C07E904100905031C07E90410082D00010105000DF31A5507050001147026531C07E904100900001C07E904100905121C07E90410090000010105000DF3225507050001147026531C07E904100A2D001C07E904100A32071C07E90410090F00010105000DF32A5507050001147026531C07E904100A2D001C07E904100A32081C07E90410091E00010105000DF3305507050001147026531C07E904100A2D001C07E904100A32091C07E90410092D00010105000DF3395507050001147026531C07E904100A2D001C07E904100A320B1C07E904100A0000010105000DF33E5507050001147026531C07E904100A2D001C07E904100A320C1C07E904100A0F00010105000DF3465507050001147026531C07E904100A2D001C07E904100A3A101C07E904100A1E00010105000DF34B5507050001147026531C07E904100B00001C07E904100B05021C07E904100A2D00010105000DF3515507050001147026531C07E904100B0F001C07E904100B14021C07E904100B0000010105000DF3575507050001147026531C07E904100B1E001C07E904100B23021C07E904100B0F00010105000DF35C5507050001147026531C07E904100B2D001C07E904100B32021C07E904100B1E00010105000DF3625507050001147026531C07E904100C00001C07E904100C05021C07E904100B2D00010105000DF3685507050001147026531C07E904100C0F001C07E904100C14021C07E904100C0000010105000DF36E5507050001147026531C07E904100C1E001C07E904100C23021C07E904100C0F00010105000DF3745507050001147026531C07E904100C2D001C07E904100C32021C07E904100C1E00010105000DF37A5507050001147026531C07E904100D00001C07E904100D05021C07E904100C2D00010105000DF3805507050001147026531C07E904100D0F001C07E904100D14021C07E904100D0000010105000DF3865507050001147026531C07E904100D1E001C07E904100D23021C07E904100D0F00010105000DF38B5507050001147026531C07E904100D2D001C07E904100D32021C07E904100D1E00010105000DF3925507050001147026531C07E904100E00001C07E904100E05021C07E904100D2D00010105000DF3975507050001147026531C07E904100E0F001C07E904100E14021C07E904100E0000010105000DF39D5507050001147026531C07E904100E1E001C07E904100E23021C07E904100E0F00010105000DF3A25507050001147026531C07E904100E2D001C07E904100E32021C07E904100E1E00010105000DF3A95507050001147026531C07E904100F00001C07E904100F05021C07E904100E2D00010105000DF3AD5507050001147026531C07E904100F0F001C07E904100F14021C07E904100F0000010105000DF3B45507050001147026531C07E904100F1E001C07E904100F23081C07E904100F0F00010105000DF3B85507050001147026531C07E904100F2D001C07E904100F32021C07E904100F1E00010105000DF3BF5507050001147026531C07E904101000001C07E904101005021C07E904100F2D00010105000DF3C35507050001147026531C07E90410100F001C07E904101014021C07E90410100000010105000DF3C95507050001147026531C07E90410101E001C07E904101023021C07E90410100F00010105000DF3CD5507050001147026531C07E90410102D001C07E904101032021C07E90410101E00010105000DF3D45507050001147026531C07E904101100001C07E904101105021C07E90410102D00010105000DF3D85507050001147026531C07E90410110F001C07E904101114021C07E90410110000010105000DF3DE5507050001147026531C07E90410111E001C07E904101123081C07E90410110F00010105000DF3E25507050001147026531C07E90410112D001C07E904101132021C07E90410111E00010105000DF3E95507050001147026531C07E904101200001C07E904101205021C07E90410112D00010105000DF3ED5507050001147026531C07E90410120F001C07E904101214021C07E90410120000010105000DF3F35507050001147026531C07E90410121E001C07E904101223021C07E90410120F00010105000DF3F85507050001147026531C07E90410122D001C07E904101232021C07E90410121E00010105000DF3FE5507050001147026531C07E904101300001C07E904101305021C07E90410122D00010105000DF4025507050001147026531C07E90410130F001C07E904101314021C07E90410130000010105000DF4085507050001147026531C07E90410131E001C07E904101323021C07E90410130F00010105000DF40D5507050001147026531C07E90410132D001C07E904101332021C07E90410131E00010105000DF4125507050001147026531C07E904101400001C07E904101405021C07E90410132D00010105000DF4175507050001147026531C07E90410140F001C07E904101414021C07E90410140000010105000DF41E5507050001147026531C07E90410141E001C07E904101423021C07E90410140F00010105000DF4245507050001147026531C07E90410142D001C07E904101432021C07E90410141E00010105000DF42C5507050001147026531C07E904101500001C07E904101505021C07E90410142D00010105000DF4315507050001147026531C07E90410150F001C07E904101514021C07E90410150000010105000DF4395507050001147026531C07E90410151E001C07E904101523021C07E90410150F00010105000DF43E5507050001147026531C07E90410152D001C07E904101532021C07E90410151E00010105000DF4465507050001147026531C07E904101600001C07E904101605021C07E90410152D00010105000DF44A5507050001147026531C07E90410160F001C07E904101614031C07E90410160000010105000DF4505507050001147026531C07E90410161E001C07E904101623031C07E90410160F00010105000DF4565507050001147026531C07E90410162D001C07E904101632031C07E90410161E00010105000DF45B5507050001147026531C07E904101700001C07E904101705021C07E90410162D00010105000DF4615507050001147026531C07E90410170F001C07E904101715091C07E90410170000010105000DF4675507050001147026531C07E90410171E001C07E904101723031C07E90410170F00010105000DF46C5507050001147026531C07E90410172D001C07E904101732031C07E90410171E00010105000DF4725507050001147026531C07E904110000001C07E9041100050E1C07E90410172D00010105000DF4780000290E16";
        //            dbUtil.dump(Frame698.parse(frame698Str));
        //            frame698Str = "680431C305106635010000640D8185038F601203000500202A02000060400200006041020000604202000150020200090020020100300201200402000040020120010200200A0200200002000010020120050200015F5507050001147026531C07E90411000F001C07E904110014021C07E904110000000109060000000000010405000010550500000CCD05000002F7050000009100010305000008480500000149050000004D010410033B1002E41003DC1003DA010312091A1209121208FE0600542505010405FFFFF4EC05FFFFF45C050000007505000000005507050001147026531C07E90411001E001C07E904110023021C07E90411000F0001090600000000000104050000105A0500000CD205000002F9050000008F0001030500000860050000014B050000004D01041003381002E01003DC1003D7010312091D12091A1209030600542510010405FFFFF4C505FFFFF435050000007605000000005507050001147026531C07E90411002D001C07E904110032021C07E90411001E000109060000000000010405000010590500000CD405000002EB050000009A00010305000008650500000146050000005301041003331002DA1003DD1003DF010312091E120914120913060054251C010405FFFFF48905FFFFF402050000007105000000005507050001147026531C07E904110100001C07E904110105021C07E90411002D000109060000000000010405000010590500000CCF05000002F905000000900001030500000864050000014C050000004B01041003331002D91003DC1003DA010312092B12091B1209010600542526010405FFFFF48C05FFFFF3FC050000007605000000005507050001147026531C07E90411010F001C07E904110114021C07E904110100000109060000000000010405000016270500000CE905000008A3050000009B00010305000008610500000608050000004D01041003E81002E710026E1003DE01031209111209081209070600542532010405FFFFFF5705FFFFF45F0500000AE005000000005507050001147026531C07E90411011E001C07E904110123021C07E90411010F0001090600000000000104050000106B0500000CDF050000030B05000000810001030500000868050000014C050000004A010410033E1002E61003DB1003D2010312091A1209141208F5060054253D010405FFFFF4FD05FFFFF462050000007F05000000005507050001147026531C07E90411012D001C07E904110132021C07E90411011E0001090600000000000104050000106B0500000CEC05000002F50500000089000103050000086F050000014A050000004701041003331002DB1003DC1003DA010312093212090D1208E80600542547010405FFFFF48005FFFFF3F1050000007705000000005507050001147026531C07E904110200001C07E904110205031C07E90411012D000109060000000000010405000010330500000CBD05000002D8050000009E000103050000085C05000001420500000053010410033D1002E91003DE1003E001031209301209051209080600542553010405FFFFF51705FFFFF497050000006B05000000005507050001147026531C07E90411020F001C07E904110214021C07E904110200000109060000000000010405000010580500000CDB05000002E1050000009B000103050000085C05000001430500000054010410032C1002D41003DE1003E2010312093712090C120909060054255E010405FFFFF44105FFFFF3C2050000006D05000000005507050001147026531C07E90411021E001C07E904110223021C07E90411020F000109060000000000010405000013A40500000CE9050000062D050000008F000103050000085905000003EF050000005101041003C51002D410029E1003D1010312093712091C1209120600542569010405FFFFFAAF05FFFFF3B605000006D905000000005507050001147026531C07E90411022D001C07E904110232021C07E90411021E000109060000000000010405000010550500000CC405000002F4050000009D000103050000085805000001490500000052010410033F1002E81003DE1003D7010312091E12091B1209160600542574010405FFFFF51505FFFFF489050000006E05000000005507050001147026531C07E904110300001C07E904110305021C07E90411022D000109060000000000010405000013DB0500000CB9050000068105000000A1000103050000084E050000026E050000005501041003CE1002E61002B91003C9010312091312092012091E060054257F010405FFFFFB5C05FFFFF48005000006B2050000002A5507050001147026531C07E90411030F001C07E904110314021C07E904110300000109060000000000010405000013010500000CDC050000059405000000910001030500000867050000033A050000004F01041003B01002E41002EC1003DA010312092612091C120906060054258A010405FFFFF96105FFFFF45505000004F205000000005507050001147026531C07E90411031E001C07E904110323021C07E90411030F0001090600000000000104050000106A0500000CC2050000030A050000009E00010305000008530500000153050000005301041003461002EE1003DF1003CD010312090812092812090C0600542595010405FFFFF55005FFFFF4BF050000006B05000000255507050001147026531C07E90411032D001C07E904110332021C07E90411031E0001090600000000000104050000108C0500000CD6050000032B050000008C000103050000086105000002C4050000004D01041003401002E41003DD1003C5010312091912092812090606005425A1010405FFFFF4F505FFFFF454050000007A05000000275507050001147026531C07E904110400001C07E904110405021C07E90411032D000109060000000000010405000012D50500000CCC050000057B050000008E00010305000008580500000329050000005201041003A81002D81002EC1003DB010312092C12091112090106005425AC010405FFFFF8E505FFFFF3F105000004DC05000000005507050001147026531C07E90411040F001C07E904110414021C07E9041104000001090600000000000104050000104A0500000CC305000002FB050000008B00010305000008680500000152050000004501041003371002DD1003DC1003D5010312091A12091B1208F306005425B6010405FFFFF4C105FFFFF42D050000007905000000005507050001147026531C07E90411041E001C07E904110423021C07E90411040F000109060000000000010405000010560500000CC0050000030B050000008B000103050000085905000001560500000047010410033E1002E41003DC1003C9010312091412093012090306005425C2010405FFFFF50705FFFFF468050000007C05000000235507050001147026531C07E90411042D001C07E904110432021C07E90411041E000109060000000000010405000012F10500000CC3050000059D0500000091000103050000084C0500000333050000004C01041003AF1002DE1002ED1003C5010312091112092312090E06005425CD0104000000005507050001147026531C07E904110500001C07E904110505021C07E90411042D0001090600000000000104050000138E0500000CC605000006370500000090000103050000085E05000003FC050000004501041003D11002EE1002A11003C401031209101209231208F606005425D8010405FFFFFBBD05FFFFF4C005000006D505000000285507050001147026531C07E90411050F001C07E904110514021C07E904110500000109060000000000010405000010510500000CCA05000002E305000000A4000103050000085E0500000143050000005301041003391002E21003DE1003DD010312091312090812091206005425E3010405FFFFF4D405FFFFF450050000006905000000005507050001147026531C07E90411051E001C07E904110523021C07E90411050F000109060000000000010405000013B50500000CCE050000065B050000008B000103050000085D0500000352050000004B01041003B21002EB1003301003D3010312090C12090C1208F806005425EE010405FFFFF93905FFFFF499050000048305000000005507050001147026531C07E90411052D001C07E904110532021C07E90411051E000109060000000000010405000014EE0500000CE105000003D80500000435000103050000085005000001BE0500000104010410036D1002E51003E81003E8010312091012090F1208F306005425FA010405FFFFF48705FFFFF456050000000005000000005507050001147026531C07E904110600001C07E904110605021C07E90411052D000109060000000000010405000014680500000CC805000004AC05000002F400010305000008540500000248050000016B010410035D1002E51003E71003E401031209071209161208E50600542612010405FFFFF3F105FFFFF46F05FFFFFFC705FFFFFFBB5507050001147026531C07E90411060F001C07E904110614021C07E90411060000010906000000000001040500001B660500000CC40500000A46050000045C0001030500000871050000056D050000024901041003E41002EB1002D71003DA01031209081208DE1208F00600542620010405FFFFFD9A05FFFFF4A705000009B005FFFFFF425507050001147026531C07E90411061E001C07E904110623021C07E90411060F000109060000000000010405000014380500000CCE05000003070500000463000103050000084905000001D30500000247010410035C1002F71003E11003DD01031208EC1208EB1208FA060054262E010405FFFFF40305FFFFF50705FFFFFFA105FFFFFF5B5507050001147026531C07E90411062D001C07E904110632021C07E90411061E000109060000000000010405000014600500000CFD05000002520500000511000103050000086B050000013405000002B401041003611002F51003E71003DF01031208F31209121208CF0600542642010405FFFFF43005FFFFF4C8050000000005FFFFFF4E5507050001147026531C07E904110700001C07E904110705021C07E90411062D000109060000000000010405000012210500000B0C0500000177050000059E000103050000079305000001A605000002CC01041003401002B01003D71003E001031208F71208F51208E206005426660104000000005507050001147026531C07E90411070F001C07E904110714031C07E90411070000010906000000000001040500000D470500000B2A0500000096050000018600010305000007A70500000055050000011F01041002FC1002B510033C1003E801031209101208E41208E706005426780104000000005507050001147026531C07E90411071E001C07E904110723031C07E90411070F0001090600000000000104050000125F0500000DB4050000041F050000008C00010305000010800500000179050000004901041003CC1003091002131003D501031209011209141208E3060054268E0104000000005507050001147026531C07E90411072D001C07E904110732031C07E90411071E00010906000000000001040500000CD20500000B21050000010F05000000A2000103050000079B05000000A5050000005301041002FB1002BE1003BD1003E101031209021208F41208F90600542697010405FFFFF52005FFFFF4B9050000005205000000005507050001147026531C07E904110800001C07E904110805021C07E90411072D000109060000000000010405000043B60500000B2E0500000103050000378500010305000007C105000000A6050000184F01041003D910029C1003C31003E801031209411208D91208E006005426A3010405FFFFF40905FFFFF390050000004905000000305507050001147026531C07E90411080F001C07E904110814021C07E90411080000010906000000000001040500000FAD0500000B140500000400050000009900010305000007AD050000034305000007E201041003B81002B71002161003E101031209151208EE1208EF06005426CA010405FFFFFAF305FFFFF48A050000065505000000005507050001147026531C07E90411081E001C07E904110823021C07E90411080F00010906000000000001040500000DA40500000C0A05000000CC05000000CD00010305000007CA0500000077050000005001041003111002DD10037B1003E7010312090B1208F91208EC06005426D3010405FFFFF53C05FFFFF4D1050000006805000000005507050001147026531C07E90411082D001C07E904110832021C07E90411081E000109060000000000010405000010890500000BCD0500000421050000009B00010305000007C70500000249050000005201041003BE1002D31002271003E001031209071208F11208F006005426DC010405FFFFFB1205FFFFF4BC050000064005000000005507050001147026531C07E904110900001C07E904110905021C07E90411082D00010906000000000001040500000C7E0500000B2C05000000B005000000A100010305000007AD0500000069050000005401041002F51002C01003691003E201031209021208EB1208F906005426E5010405FFFFF53505FFFFF4BF050000006305000000005507050001147026531C07E90411090F001C07E904110914021C07E90411090000010906000000000001040500000CBF0500000B6505000000B905000000A1000103050000079D0500000068050000005001041002F31002BE1003741003D401031208FA12090012090406005426EE010405FFFFF4F105FFFFF46F050000006205000000005507050001147026531C07E90411091E001C07E904110923031C07E90411090F00010906000000000001040500000CAA0500000B4C05000000C4050000009A0001030500000797050000006B050000004F01041002F71002C010037A1003C601031208F41209181208FB06005426F8010405FFFFF52705FFFFF4980500000064050000002A5507050001147026531C07E90411092D001C07E904110932031C07E90411091E00010906000000000001040500000F820500000E4205000000A4050000009B00010305000007BA050000005E050000004E01041003371003111003441003D6010312090E1209141209020600542700010405FFFFF55105FFFFF4C7050000006C05000000005507050001147026531C07E904110A00001C07E904110A05021C07E90411092D00010906000000000001040500000FEB0500000B6F05000003E2050000009B00010305000007A80500000355050000004E01041003BE1002BF1001F51003D501031209111209171209060600542709010405FFFFFB3F05FFFFF46C05000006B405000000005507050001147026531C07E904110A0F001C07E904110A14031C07E904110A0000010906000000000001040500000FAA0500000E6205000000B1050000009700010305000008080500000060050000004D010410033710031110035A1003C801031209121209151209030600542712010405FFFFF53B05FFFFF4AA050000006A05000000275507050001147026531C07E904110A1E001C07E904110A23031C07E904110A0F00010906000000000001040500000CA60500000B72050000009A050000009A00010305000007AF0500000058050000005201041002EB1002BA1003321003E301031209131208FF1208F9060054271B010405FFFFF4BC05FFFFF440050000006C05000000005507050001147026531C07E904110A2D001C07E904110A32031C07E904110A1E00010906000000000001040500000F790500000BB4050000032005000000A400010305000007BE05000002740500000056010410038F1002C110022D1003E0010312091D1208F812091C0600542725010405FFFFF8FC05FFFFF43D05000004A905000000005507050001147026531C07E904110B00001C07E904110B05021C07E904110A2D00010906000000000001040500000CF70500000BBC050000009705000000A500010305000008310500000059050000005401041002F41002C31003301003DD010312091B12090E120925060054272E010405FFFFF4C805FFFFF444050000006B05000000005507050001147026531C07E904110B0F001C07E904110B14021C07E904110B0000010906000000000001040500000CF50500000BC00500000097050000009E00010305000007F4050000005A050000005101041002ED1002BD1003291003E3010312092812091512090D0600542739010405FFFFF48C05FFFFF40E050000006E05000000005507050001147026531C07E904110B1E001C07E904110B23021C07E904110B0F00010906000000000001040500000C970500000B4505000000BD050000009500010305000007BA0500000067050000004D01041002EC1002B31003571003BA010312091D12093A1209110600542742010405FFFFF4D505FFFFF4330500000073050000002F5507050001147026531C07E904110B2D001C07E904110B32021C07E904110B1E00010906000000000001040500000C9E0500000B5505000000AB050000009E00010305000007D1050000025A050000005001041002EB1002B510034C1003CD010312091D12091F120917060054274C010405FFFFF4C905FFFFF436050000006D05000000265507050001147026531C07E904110C00001C07E904110C05021C07E904110B2D00010906000000000001040500000DB40500000B7105000001A2050000009E0001030500000862050000010D050000005301041003001002B31003C71003DD010312093412091A1209150600542757010405FFFFF49905FFFFF411050000006F05000000005507050001147026531C07E904110C0F001C07E904110C14021C07E904110C00000109060000000000010405000012760500000CDB05000004F205000000A8000103050000082105000003B7050000005501041003C61002DB1002451003DC010312094B1209311209470600542762010405FFFFFB1405FFFFF40A05000006EE05000000005507050001147026531C07E904110C1E001C07E904110C23021C07E904110C0F00010906000000000001040500000E010500000BAA05000001BD050000009900010305000008310500000111050000004C01041002FE1002AE1003C51003C4010312094012094E12092C060054276C010405FFFFF44205FFFFF39E0500000079050000002B5507050001147026531C07E904110C2D001C07E904110C37011C07E904110C1E000109060000000000010405000019AE050000116205000007A705000000A40001030500000A0A05000005FC050000005401041003E81003371002231003D9010312095B12093A120943060054277A0104050000000005FFFFF4010500000BB505000000005507050001147026531C07E904110D00001C07E904110D051A1C07E904110C2D00010906000000000001040500000E630500000C1E05000001A205000000A300010305000007F9050000010F050000005501041002FF1002B51003C11003D8010312095412093D12094C0600542786010405FFFFF3FB05FFFFF365050000007705000000005507050001147026531C07E904110D0F001C07E904110D14021C07E904110D000001090600000000000104050000137E0500000DC7050000051D050000009900010305000007BB0500000370050000004F01041003D010030910025F1003D7010312093712091C12092C0600542790010405FFFFFBA805FFFFF4DA05000006B005000000005507050001147026531C07E904110D1E001C07E904110D23021C07E904110D0F000109060000000000010405000010320500000EF3050000009A05000000A4000103050000088A050000005B0500000054010410033A1003151003171003D8010312095312093812093F060054279C010405FFFFF4FD05FFFFF467050000007705000000005507050001147026531C07E904110D2D001C07E904110D32021C07E904110D1E00010906000000000001040500000D980500000C5805000000A4050000009C00010305000007FA050000005D050000005201041002FA1002C810031F1003D4010312095B12094712094106005427A60104000000005507050001147026531C07E904110E00001C07E904110E05021C07E904110D2D00010906000000000001040500000DD20500000C9505000000A90500000094000103050000083D05000001D9050000004D01041003061002D51003211003D1010312094A12093A12092906005427B00104000000005507050001147026531C07E904110E0F001C07E904110E14021C07E904110E0000010906000000000001040500000EDB0500000D99050000009F05000000A3000103050000081A050000005B050000005301041003271002FD1003251003D7010312094612093A12093C06005427BA0104000000005507050001147026531C07E904110E1E001C07E904110E23021C07E904110E0F00010906000000000001040500000D570500000C1005000000AA050000009C00010305000007DF050000005F050000004E01041002F91002C51003311003CD010312094712094512093406005427C40104000000005507050001147026531C07E904110E2D001C07E904110E32021C07E904110E1E00010906000000000001040500000D520500000C0B05000000AF050000009800010305000007E40500000061050000004D01041002EB1002B610032B1003CD010312094812094B12093306005427CD010405FFFFF42305FFFFF380050000007E05000000255507050001147026531C07E904110F00001C07E904110F05021C07E904110E2D00010906000000000001040500000F500500000E0905000000B00500000097000103050000082C0500000062050000004D01041003261002FB1003361003C6010312094612094E12093306005427D7010405FFFFF4CF05FFFFF42B050000007B05000000285507050001147026531C07E904110F0F001C07E904110F14021C07E904110F0000010906000000000001040500000CF80500000BB405000000A005000000A300010305000007C1050000005C050000005101041002E91002B51003301003DC010312093312092F12092906005427E1010405FFFFF46105FFFFF3D6050000007105000000005507050001147026531C07E904110F1E001C07E904110F23021C07E904110F0F00010906000000000001040500000E0C0500000CCA05000000A105000000A100010305000007F0050000005C050000005001041003161002E910032D1003D4010312093A12093712093006005427EB010405FFFFF51E05FFFFF489050000007305000000005507050001147026531C07E904111000001C07E904111005081C07E904110F2D00010906000000000001040500000D500500000B96050000011B050000009F00010305000007F405000000A7050000005101041002F71002B71003A01003D6010312093E12093512093206005428010104000000005507050001147026531C07E90411100F001C07E904111014031C07E904111000000109060000000000010405000012470500000CB605000004E905000000A800010305000008C1050000027F050000005301041003BF1002C710023C1003CB010312094612094212094B060054280C0104000000005507050001147026531C07E90411101E001C07E904111023031C07E90411100F00010906000000000001040500000EB50500000D0E05000001260500000081000103050000085D05000000A6050000004801041003111002DB1003991003D0010312093312092D12090406005428180104000000005507050001147026531C07E90411102D001C07E904111032031C07E90411101E000109060000000000010405000017830500000FC7050000072E050000008E0001030500000A0F05000005CE050000004A01041003E71003111002131003C9010312092D1209341209190600542823010405FFFFFF3F05FFFFF3A20500000B7905000000245507050001147026531C07E904111100001C07E904111105021C07E90411102D000109060000000000010405000013B4050000122E05000000E505000000A10001030500000B1E05000000870500000051010410036610034A1003941003DA010312091012090C1209110600542831010405FFFFF4EA05FFFFF469050000006405000000005507050001147026531C07E90411110F001C07E904111114021C07E904111100000109060000000000010405000014CC0500000D7505000006C1050000009600010305000008FD0500000584050000004E01041003E61002ED10021E1003E301031209161209001208F4060054283D010405FFFFFEA505FFFFF41D0500000A7905000000005507050001147026531C07E90411111E001C07E904111123021C07E90411110F00010906000000000001040500000F280500000DB605000000D9050000009A00010305000008C6050000007C050000004D010410033410030A10038E1003D901031208F31208FC1208EA0600542848010405FFFFF56E05FFFFF4F0050000006305000000005507050001147026531C07E90411112D001C07E904111132021C07E90411111E00010906000000000001040500004B920500000EBC050000067B050000365C00010305000008D4050000054A05000017E201041003E81003131002221003E801031209211208CF1208DC060054285B010405FFFFFF5405FFFFF47105000009F405000000EF5507050001147026531C07E904111200001C07E904111205021C07E90411112D00010906000000000001040500000D4A0500000C1005000000AB050000008F00010305000007B40500000067050000004B01041003191002EE1003601003E501031208EC1208E71208D1060054286A010405FFFFF5CB05FFFFF55A050000006405000000005507050001147026531C07E90411120F001C07E904111214021C07E9041112000001090600000000000104050000129D0500000B660500000698050000009F00010305000007AD0500000455050000005101041003E61002D51002321003E301031208CE1208BD1208D50600542874010405FFFFFEF505FFFFF53005000009B405000000005507050001147026531C07E90411121E001C07E904111223021C07E90411120F00010906000000000001040500000C930500000B60050000009D0500000096000103050000079F050000005A050000004E01041003011002D210035B1003DF01031208E51208E11208DD060054287D010405FFFFF58A05FFFFF517050000005E05000000005507050001147026531C07E90411122D001C07E904111232021C07E90411121E00010906000000000001040500000FCE0500000B5005000003E1050000009C000103050000079B0500000331050000004F01041003C01002CE1002131003D501031208EF1208EA1208F30600542887010405FFFFFB5D05FFFFF50C050000063105000000005507050001147026531C07E904111300001C07E904111305021C07E90411122D00010906000000000001040500000C800500000B4505000000A4050000009700010305000007C3050000005B050000004D01041002F81002C61003601003D901031208E71208E61208E20600542890010405FFFFF54E05FFFFF4D3050000006005000000005507050001147026531C07E90411130F001C07E904111314021C07E90411130000010906000000000001040500000CF40500000BB505000000A8050000009700010305000007ED050000005C050000004D010410030C1002DD10035A1003DD01031208F81208F61208E80600542899010405FFFFF59E05FFFFF522050000006505000000005507050001147026531C07E90411131E001C07E904111323021C07E90411130F00010906000000000001040500000E240500000CEA05000000A10500000099000103050000080C050000005C050000004E01041003261002FD1003521003DD01031208F91208F61208EB06005428A3010405FFFFF5A205FFFFF527050000006405000000005507050001147026531C07E90411132D001C07E904111332021C07E90411131E0001090600000000000104050000101E0500000B1C050000009B050000046600010305000007A3050000005A05000001EA01041003391002B71003471003E8010312090A1208F41208F006005428AD010405FFFFF4F805FFFFF485050000006505000000005507050001147026531C07E904111400001C07E904111405021C07E90411132D0001090600000000000104050000135C0500000DE405000001210500000457000103050000085E05000000A605000001EB010410036710030C1003DE1003E801031208F71208E61208E506005428B9010405FFFFF50F05FFFFF4D8050000002A05000000005507050001147026531C07E90411140F001C07E904111414021C07E904111400000109060000000000010405000039670500002C71050000016E0500000B8800010305000014E305000000DA050000054701041003D61003CF1003E81003E501031208E11208F81208D706005428D2010405FFFFF51305FFFFF5EC050000000005FFFFFF1F5507050001147026531C07E90411141E001C07E904111423021C07E90411140F00010906000000000001040500006C9E0500002B25050000378D05000009EC00010305000013D4050000188A050000050901041003E31003CD1003E81003E601031208CD1208D91208E706005428FE010405FFFFF55005FFFFF5DA050000002D05FFFFFF4A5507050001147026531C07E90411142D001C07E904111432021C07E90411141E000109060000000000010405000051130500000E3705000038DD05000009FF00010305000008680500001948050000048801041003DD1003201003E81003E601031208DA1208CA1209010600542940010405FFFFF42405FFFFF55905FFFFFF6E05FFFFFF5D5507050001147026531C07E904111500001C07E904111505021C07E90411142D00010906000000000001040500001F640500000E85050000073E05000009A100010305000008670500000461050000047001041003D61003371003461003E501031208C51208E11208CA060054295C010405FFFFF9FE05FFFFF5FE05000004B605FFFFFF495507050001147026531C07E90411150F001C07E904111514021C07E90411150000010906000000000001040500001AE00500000E07050000033F050000099A000103050000087D0500000243050000046D010410039210031F1003D51003E501031208E31208EF1208CF060054296F010405FFFFF40B05FFFFF57305FFFFFF5B05FFFFFF3D5507050001147026531C07E90411151E001C07E904111523021C07E90411150F000109060000000000010405000022BC050000112C050000067F0500000B1100010305000008DB05000003FB050000051301041003D910035510030D1003E601031208EA1208EA1208CF0600542983010405FFFFF9F305FFFFF587050000053405FFFFFF395507050001147026531C07E90411152D001C07E904111532021C07E90411151E00010906000000000001040500001AF3050000102E0500000252050000087200010305000008B3050000018905000003FA01041003951003451003DF1003E3010312090F1208D51208D60600542995010405FFFFF45005FFFFF57005FFFFFFB205FFFFFF2E5507050001147026531C07E904111600001C07E904111605031C07E90411152D00010906000000000001040500001DAD05000010EA05000005D405000006EF00010305000008CF05000003C7050000035301041003D31003511002F11003E201031208EC1208D51208E406005429A6010405FFFFF9D105FFFFF581050000051905FFFFFF375507050001147026531C07E90411160F001C07E904111614031C07E904111600000109060000000000010405000020990500000F8D050000077505000009980001030500000935050000026B050000044701041003D51003481003741003E701031208D61208F21208DB06005429B8010405FFFFF98E05FFFFF5F405000003EF05FFFFFFAA5507050001147026531C07E90411161E001C07E904111623031C07E90411160F00010906000000000001040500001EEB05000010D905000008CA0500000549000103050000090305000004C9050000025701041003DC10035110034A1003E801031209071208F11208DF06005429CA010405FFFFFB2F05FFFFF595050000059F05000000005507050001147026531C07E90411162D001C07E904111632031C07E90411161E00010906000000000001040500001AD80500000DFF05000003EC05000008EE000103050000086F05000001FB05000003EE01041003A11003111003E41003E801031209061208F21208ED06005429F0010405FFFFF55005FFFFF4F9050000005A05000000005507050001147026531C07E904111700001C07E904111705021C07E90411162D00010906000000000001040500001DE3050000124505000006390500000565000103050000094305000003DD050000025D01041003E01003701002C81003E801031208F71208EC1208EA0600542A02010405FFFFFC4805FFFFF629050000062505000000005507050001147026531C07E90411170F001C07E904111715081C07E90411170000010906000000000001040500001AB805000012470500000314050000055D00010305000009680500000160050000025B01041003AF1003701003E01003E801031208E81209031208EB0600542A14010405FFFFF68805FFFFF61E050000006405000000005507050001147026531C07E90411170F001C07E9041117150F1C07E90411170F00010906000000000001040500001B1C05000012B105000003270500000544000103050000099605000003FB050000025201041003B01003721003DF1003E801031208F71209091208E10600542A27010405FFFFF67F05FFFFF606050000006E05000000005507050001147026531C07E90411172D001C07E904111732021C07E90411171E000109060000000000010405000016FD050000126505000002FD050000019B0001030500000952050000015505000000BF010410039D1003711003DF1003E801031208FB1208FF1208F50600542A37010405FFFFF69005FFFFF620050000006605000000005507050001147026531C07E904120000001C07E9041200050D1C07E90411172D000109060000000000010405000016E6050000126805000002F905000001850001030500000950050000015205000000B5010410039B10036F1003DD1003E801031209031209051208D90600542A47010405FFFFF67705FFFFF600050000007105000000000000208B16";
        //            dbUtil.dump(Frame698.parse(frame698Str));
        //            frame698Str = "68D000C30511011065474176E5EC8503A5601203000500202A020000604002000060410200006042020001500202000C0010020100200201005002010060020100700201008002012017020020180200200002002001020020040200200A020001015507054147651001111C07E9031C1700001C07E9031C170A071C07E9031C162D00010C0000000000000000010312000012000012090E01030500000000050000000005000000000104050000000005000000000500000000050000000001041003E81003E81003E81003E80000BB3316";
        //            dbUtil.dump(Frame698.parse(frame698Str));
        //            frame698Str = "685001C30510663501000064FF1D85038F601203000500202A0200006040020000604102000060420200015004020008002002000070020000500200004002000080020000600200003002000010020001015507050001147026531C07E904150000001C07E904150005041C07E9041400000001080105060000000006000000000600000000060000000006000000000105060000000006000000000600000000060000000006000000000105060008B17E060000000006000281430600033814060002F82601050500000000050000000005000000000500000000050000000001050600054B640600000000060001FF14060001AD7E0600019ED2010506000000000600000000060000000006000000000600000000010505000DFCE205000000000500048057050004E59205000496F80105060054393F0600000000060024F3BA060017F332060017525200004F9216"
        //            dbUtil.dump(Frame698.parse(frame698Str));
    }

    @ResponseBody
    @GetMapping(value = "/foo/{date}")
    public ResponseEntity<String> foo(@PathVariable String date) {
        String oi = "2000";
        log.info(BODY_IN, new Exception().getStackTrace()[0].getMethodName(), String.format("%s/%s", date, oi));

        int    statusCode = HttpStatus.OK.value();
        String respBody   = "";
        try {
            List<Map<String, Object>> xxx = dbUtil.getMeters(date, oi);

            EChartGaps eChartGaps = new EChartGaps();
            eChartGaps.setTitle(String.format("[%s] 采集统计图", OINames.get(oi)));
            eChartGaps.setName(String.format("%s", date));
            for (var x : xxx) {
                eChartGaps.getLabs().add((String) x.get("meter_addr"));
                eChartGaps.getVals().add(Double.parseDouble(x.get("cnt").toString()));
            }

            respBody = mapper.writeValueAsString(eChartGaps);
        } catch (Exception e) {
            log.error("getNodeAbandonRate failed.", e);

            Thread.currentThread().interrupt();
            statusCode = HttpStatus.SERVICE_UNAVAILABLE.value();
            respBody   = HttpStatus.SERVICE_UNAVAILABLE.name();
        }

        log.info(BODY_OUT, new Exception().getStackTrace()[0].getMethodName(), statusCode, respBody);
        return ResponseEntity.status(statusCode).body(respBody);
    }

    @ResponseBody
    @GetMapping(value = "/foobar/{date}/{meterAddr}")
    public ResponseEntity<String> foobar(@PathVariable String date,
                                         @PathVariable String meterAddr) {
        String oi = "2000";
        log.info(BODY_IN, new Exception().getStackTrace()[0].getMethodName(),
                String.format("%s/%s/%s", date, meterAddr, oi));

        int    statusCode = HttpStatus.OK.value();
        String respBody   = "";
        try {
            List<Map<String, Object>> xxx = dbUtil.getV(date, meterAddr, oi);

            EChartGaps eChartGaps = new EChartGaps();
            eChartGaps.setTitle(String.format("%s-%s 采集曲线图", meterAddr, OINames.get(oi)));
            eChartGaps.setName(String.format("%s", date));
            for (var x : xxx) {
                int points = Integer.parseInt(x.get("points").toString());
                TreeMap<String, String> datas = mapper.readValue(x.get("v").toString(),
                        new TypeReference<TreeMap<String, String>>() {
                        });

                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

                String        dateStr  = "2021-01-01 00:00:00";
                LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);

                DateTimeFormatter formatterKey = DateTimeFormatter.ofPattern("HH:mm:ss");
                DateTimeFormatter formatterVal = DateTimeFormatter.ofPattern("HH:mm:ss");
                for (int i = 0; i < points; i++) {
                    String key = dateTime.format(formatterKey);

                    eChartGaps.getLabs().add(dateTime.format(formatterVal));
                    if (datas.get(key) == null) {
                        eChartGaps.getVals().add(null);
                    } else {
                        eChartGaps.getVals().add(Double.parseDouble(datas.get(key)));
                    }

                    dateTime = dateTime.plusMinutes(1440 / points);
                }
                break;
            }

            respBody = mapper.writeValueAsString(eChartGaps);
        } catch (Exception e) {
            log.error("getNodeLog failed.", e);

            Thread.currentThread().interrupt();
            statusCode = HttpStatus.SERVICE_UNAVAILABLE.value();
            respBody   = HttpStatus.SERVICE_UNAVAILABLE.name();
        }

        log.info(BODY_OUT, new Exception().getStackTrace()[0].getMethodName(), statusCode, respBody);
        return ResponseEntity.status(statusCode).body(respBody);
    }

    public void asyncTask() {
        ThreadPoolExecutor ex = (ThreadPoolExecutor) executor;

        long taskCount = ex.getQueue().size();

        log.debug("asyncTask {}. {}", taskCount, Util.getUUID());
    }
}
